﻿using GalaSoft.MvvmLight;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace MVVMAppINotifyDataErrorInfo.ViewModel
{   
    public class MainViewModel : ViewModelNotifyDataError
    {
        #region Constructor

        public MainViewModel()
        {
            List<string> operatorDependencies = new List<string>();
            operatorDependencies.Add("ShiftSupervisor");
            PropertyDependencies.Add("EquipmentOperator", operatorDependencies);

            List<string> supervisorDependencies = new List<string>();
            supervisorDependencies.Add("EquipmentOperator");
            PropertyDependencies.Add("ShiftSupervisor", supervisorDependencies);
        }

        #endregion
        #region Properties

        string _equipment = string.Empty;
        public string Equipment
        {
            get { return _equipment; }
            set
            {
                if (_equipment != value)
                {
                    _equipment = value;
                    RaisePropertyChanged("Equipment");
                    ValidateProperty("Equipment");
                }
            }
        }

        string _runningHours = string.Empty;
        public string RunningHours
        {
            get { return _runningHours; }
            set
            {
                if (_runningHours != value)
                {
                    _runningHours = value;
                    RaisePropertyChanged("RunningHours");
                    ValidateProperty("RunningHours");
                }
            }
        }

        string _equipmentOperator = string.Empty;
        public string EquipmentOperator
        {
            get { return _equipmentOperator; }
            set
            {
                if (_equipmentOperator != value)
                {
                    _equipmentOperator = value;
                    RaisePropertyChanged("EquipmentOperator");
                    ValidateProperty("EquipmentOperator");
                }
            }
        }

        string _shiftSupervisor = string.Empty;
        public string ShiftSupervisor
        {
            get { return _shiftSupervisor; }
            set
            {
                if (_shiftSupervisor != value)
                {
                    _shiftSupervisor = value;
                    RaisePropertyChanged("ShiftSupervisor");
                    ValidateProperty("ShiftSupervisor");
                }
            }
        }

        ICommand _logRunningHoursCommand = null;
        public ICommand LogRunningHoursCommand
        {
            get
            {
                if (_logRunningHoursCommand == null)
                {
                    _logRunningHoursCommand = new RelayCommand(
                                () =>
                                { 
                                    ValidateAll();
                                });
                }

                return _logRunningHoursCommand;
            }
        }

        #endregion

        #region Overriden methods

        protected override IEnumerable<string> ValidatePropertySpecialized(string propertyName)
        {
            List<string> errorMessages = new List<string>();
            //IEnumerable<string> ret = null;

            if (!string.IsNullOrEmpty(propertyName))
            {
                object propertyValue =
                    this.GetType()
                    .GetProperty(propertyName)
                    .GetValue(this, null);

                if (propertyValue == null ||
                    propertyValue.ToString()
                                 .Trim()
                                 .Equals(string.Empty))
                {
                    errorMessages.Add(string.Format("{0} is required. ",propertyName));

                    
                }
            }

            switch(propertyName)
            {
                case "RunningHours":
                    double runningHours;
                    double.TryParse(_runningHours, out runningHours);
                    if (runningHours == 0 && _runningHours.Length > 0)
                    {
                        string msg = (_runningHours != null && _runningHours.Equals("0")) ?
                                        "Zero not allowed for running hours." :
                                        "Only numeric running hours are allowed!";

                        errorMessages.Add(msg);
                    }                   
                    break;

                case "EquipmentOperator":
                case "ShiftSupervisor":
                    if (!string.IsNullOrEmpty(_equipmentOperator) && _equipmentOperator.Equals(_shiftSupervisor))
                    {
                        errorMessages.Add("EquipmentOperator and ShiftSupervisor must be different");
                    }
                    break;
                case "Equipment":
                    //some other validations
                    break;
                default:
                    //other validations if required
                    break;
            }

            return errorMessages.Count > 0 ? errorMessages.AsEnumerable<string>() : null;
        }


        private void ValidateAll()
        {
            ValidateProperty("Equipment");
            ValidateProperty("RunningHours");
            ValidateProperty("EquipmentOperator");
            ValidateProperty("ShiftSupervisor");
        }

        #endregion
    }
}